package com.app.serve.api.service;

import com.app.serve.api.entity.User;
import com.app.serve.api.mapper.UserMapper;
import com.app.serve.enums.ErrorCodeEnum;
import com.app.serve.exception.JsonRespException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public User findById() {
        return userMapper.selectByPrimaryKey(1);
    }

    public void register(String username, String password) throws JsonRespException {
        // 查询重名
        User result = userMapper.selectByUserName(username);
        if(result != null ) {
            throw new JsonRespException(ErrorCodeEnum.USERNAME_EXISTED);
        }

        // 写到数据库
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        int count = userMapper.insertSelective(user);
        if(count == 0) {
            throw new JsonRespException(ErrorCodeEnum.INSERT_FAILED);
        }
    }
}
